/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package components;

import dao.OwnerDAO;
import entity.Owner;
import hpvas.HPVAS;
import java.text.ParseException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.Persistence;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author Juan
 */
public class POwnerUpdate extends javax.swing.JPanel {
    
    private Owner currentOwner = null;

    /**
     * Creates new form POwnerRegistration
     */
    public POwnerUpdate() {
        initComponents();
        initialize();
    }
    
    private void initialize() {
        FTFSSN.setFormatterFactory(new JFormattedTextField.AbstractFormatterFactory() {

            @Override
            public JFormattedTextField.AbstractFormatter getFormatter(JFormattedTextField tf) {
                MaskFormatter mf = null;
                try {
                    mf = new MaskFormatter("###-##-####");
                } catch (ParseException ex) {
                    Logger.getLogger(POwnerUpdate.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    return mf;
                }
            }
        });
        
        SBirthday.setEditor(new JSpinner.DateEditor(SBirthday, "dd/MM/yyyy"));
        SBirthday.setValue(new Date());
        
        ComponentFactory.attachVerifier("Please provide the name of the owner.", TFName);
        ComponentFactory.attachVerifier("Please provide the last name of the owner.", TFLastname);
        ComponentFactory.attachVerifier("Please provide the birthplace of the owner.", TFBirthplace);
    }
    
    public POwnerUpdate(Owner owner) {
        this.currentOwner = owner;
        initComponents();
        initialize();
        TFName.setText(owner.getName());
        TFLastname.setText(owner.getLastname());
        FTFSSN.setValue(owner.getSSN());
        SBirthday.setValue(owner.getBirthday());
        TFBirthplace.setText(owner.getBirthplace());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        FTFSSN = new javax.swing.JFormattedTextField();
        TFName = new javax.swing.JTextField();
        TFLastname = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        SBirthday = new javax.swing.JSpinner();
        jLabel6 = new javax.swing.JLabel();
        TFBirthplace = new javax.swing.JTextField();
        BUpdateOwner = new javax.swing.JButton();
        BCancel = new javax.swing.JButton();

        jLabel1.setText("Update Owner:");

        jLabel2.setText("Name:");

        jLabel3.setText("Lastname:");

        jLabel4.setText("SSN:");

        FTFSSN.setText("XXX-XX-XXXX");

        jLabel5.setText("Birthday:");

        SBirthday.setModel(new javax.swing.SpinnerDateModel(new java.util.Date(), null, new java.util.Date(), java.util.Calendar.DAY_OF_MONTH));

        jLabel6.setText("Birthplace:");

        BUpdateOwner.setText("Update Owner");
        BUpdateOwner.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BUpdateOwnerActionPerformed(evt);
            }
        });

        BCancel.setText("Cancel");
        BCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(TFName)
                    .addComponent(FTFSSN)
                    .addComponent(TFLastname)
                    .addComponent(TFBirthplace)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(SBirthday, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(BUpdateOwner)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(BCancel)))
                        .addGap(0, 188, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TFName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TFLastname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(FTFSSN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(SBirthday, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TFBirthplace, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BUpdateOwner)
                    .addComponent(BCancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void BCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BCancelActionPerformed
        HPVAS.closeCurrentTab();
    }//GEN-LAST:event_BCancelActionPerformed

    private void BUpdateOwnerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BUpdateOwnerActionPerformed
        CustomVerifier currentVerifier;
        
        currentVerifier = (CustomVerifier) TFName.getInputVerifier();
        if (currentVerifier.isValid()) {
            currentOwner.setName(TFName.getText().trim());
        } else {
            return;
        }
        
        currentVerifier = (CustomVerifier) TFLastname.getInputVerifier();
        if (currentVerifier.isValid()) {
            currentOwner.setLastname(TFLastname.getText().trim());
        } else {
            return;
        }
        
        if (FTFSSN.isEditValid()) {
            currentOwner.setSSN(FTFSSN.getText());
        } else {
            JOptionPane.showMessageDialog(this.getTopLevelAncestor(), "The social security number provided is not valid.", "Warning", JOptionPane.WARNING_MESSAGE);
            FTFSSN.requestFocusInWindow();
            return;
        }
        
        SpinnerDateModel sdm = (SpinnerDateModel) SBirthday.getModel();
        currentOwner.setBirthday(new java.sql.Date(sdm.getDate().getTime()));
        
        currentVerifier = (CustomVerifier) TFBirthplace.getInputVerifier();
        if (currentVerifier.isValid()) {
            currentOwner.setBirthplace(TFBirthplace.getText());
        } else {
            return;
        }
        
        OwnerDAO ownerDAO = new OwnerDAO(Persistence.createEntityManagerFactory("HPVA"));
        
        try {
            ownerDAO.edit(currentOwner);
            JOptionPane.showMessageDialog(this.getTopLevelAncestor(),
                "The owner has been succesfully updated.", "Info", JOptionPane.INFORMATION_MESSAGE);
            HPVAS.loadOwners();
            HPVAS.closeCurrentTab();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this.getTopLevelAncestor(),
                    "The owwner couldn't be modified.", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_BUpdateOwnerActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BCancel;
    private javax.swing.JButton BUpdateOwner;
    private javax.swing.JFormattedTextField FTFSSN;
    private javax.swing.JSpinner SBirthday;
    private javax.swing.JTextField TFBirthplace;
    private javax.swing.JTextField TFLastname;
    private javax.swing.JTextField TFName;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    // End of variables declaration//GEN-END:variables
}
